package com.yc.bos.web.action;

import com.yc.bos.model.Region;
import com.yc.bos.model.Subarea;
import com.yc.bos.service.ISubareaService;
import com.yc.bos.web.action.base.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

@Controller  //action注解
public class SubareaAction extends BaseAction<Subarea>{
    @Autowired
    private ISubareaService subareaService;
    @Override
    public String save() {
        System.out.println("数据1SubareaAction-getmodel():" + getModel());
        System.out.println("数据1:" + getModel().getRegion());
        subareaService.save(getModel());
        return SUCCESS;
    }

    @Override
    public String update() {
        return null;
    }

    @Override
    public String delete() throws IOException {
        return null;
    }

    @Override
    public String list() {
        return null;
    }

    public  void  pageQuery() throws IOException {
        pd.setCurrentPage(page);
        pd.setPageSize(rows);
        DetachedCriteria dc=pd.getDetachedCriteria();
        String addresskey=getModel().getAddresskey();
        if(!StringUtils.isEmpty(addresskey)){
            dc.add(Restrictions.like("addresskey","%"+addresskey+"%"));
        }
        Region region=getModel().getRegion();
        if (region!=null){
            String province=region.getProvince();
            String city=region.getCity();
            String   district=region.getCity();
            dc.createAlias("region","r");//创建一个别名
            if(!StringUtils.isEmpty(province)){
                dc.add(Restrictions.like("r.province","%"+province+"%"));
            }
            if (!StringUtils.isEmpty(city)){
                dc.add(Restrictions.like("r.city","%"+city+"%"));
            }
            if (!StringUtils.isEmpty(district)){
                dc.add(Restrictions.like("r.district","%"+district+"%"));
            }
        }
        subareaService.pageQuery(pd);
        responseJson(pd,new String[]{"currentPage","pageSize","detachedCriteria","subareas"});


    }

    //导出表格【下载一个xls文件】
    public void exportExcle() throws IOException {
        //准备xls文件【输入流】
        //1.创建wookbook【相当于xls文件】
        HSSFWorkbook workbook=new HSSFWorkbook();
        HSSFSheet sheet=workbook.createSheet("分区数据");
        HSSFRow row=sheet.createRow(0);
        row.createCell(0).setCellValue("分区编码");
        row.createCell(1).setCellValue("区域编码");
        row.createCell(2).setCellValue("关键字");
        row.createCell(3).setCellValue("省市区");

        List<Subarea> subareas=subareaService.findAll();
        for(Subarea s:subareas){
            row=sheet.createRow(sheet.getLastRowNum()+1);
            row.createCell(0).setCellValue(s.getId());
            row.createCell(1).setCellValue(s.getRegion().getId());
            row.createCell(2).setCellValue(s.getAddresskey());
            row.createCell(3).setCellValue(s.getRegion().getName());
        }
        HttpServletResponse response= ServletActionContext.getResponse();
        String  fileName= URLEncoder.encode("分区数据.xls","utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        String contentType=ServletActionContext.getServletContext().getMimeType(fileName);
        System.out.println(contentType);
        response.setContentType(contentType);
        OutputStream os=response.getOutputStream();
        workbook.write(os);

    }

    public  void  listJson() throws IOException {
        //未绑定的分区数据【也就是这个分区没有指向一个员工负责】
        List<Subarea> list=subareaService.findAllWithUnbind();
        responseJson(list,new String[]{"decidedzone","region"});
    }

}
